from django.core.cache import cache
from rest_framework import serializers
from userManage import models


class UserSeriallizer(serializers.ModelSerializer):
    """
    用户序列化
    """

    class Meta:
        model = models.Loginuser
        fields = ['id', 'name', 'cate', 'headshot']

class UserListSeriallizer(serializers.ModelSerializer):
    """
    用户序列化
    """
    online = serializers.SerializerMethodField()
    def get_online(self, insatnce):
        onli = cache.get(str(insatnce.id), None)
        if onli:
            return 1
        else:
            return 0
    class Meta:
        model = models.Loginuser
        fields = ['id', 'name', 'cate', 'headshot', 'online']